#include "MainThread.h"
#include "log_api.h"

MainThread::MainThread():OSThread(),
     m_pServer(NULL)
{
    
}
MainThread::~MainThread()
{
    LOG_OUTPUT("~MainThread Start\n");
    m_pServer->StopRun(NULL);
    LOG_OUTPUT("~MainThread End\n");
}
void MainThread::SetStreamServer(StreamServer* pServer)
{
    m_pServer = pServer;
}
int MainThread::Initialize()
{
    printf("MainThread::Initialize()\n");
    //m_v4l2Capture.Initialize(/*m_pServer*/);
    //m_pAlprThread.Initialize(m_pServer);


    //Start();
    return 0;
}
void MainThread::Entry()
{
    LOG_OUTPUT("MainThread Start\n");
    m_v4l2Capture.Start();
    OSMutex *pMutex = m_v4l2Capture.GetMutex();
    OSCond *pCond = m_v4l2Capture.GetCond();
    //LOG_OUTPUT("MainThread:mutex=%p",&mutex);
    //LOG_OUTPUT("MainThread:cond=%p",&cond);
    fprintf(stderr,"MainThread:mutex=%p\n",pMutex);
    fprintf(stderr,"MainThread:cond=%p\n",pCond);
    char msg[1024]={"hello world"};
    unsigned char* framebuffer;
    int framesize = 0;
    //OSThread::Sleep(1000);
    while(1)
    {
        fprintf(stderr,"MainThread:mutex=%p\n",pMutex);
        fprintf(stderr,"MainThread:cond=%p\n",pCond);
        OSMutexLocker locker(pMutex);
        
        //mutex.Lock();
        pCond->Wait(pMutex);
	    //LOG_OUTPUT("Signal");
        framebuffer = m_v4l2Capture.GetFrameBuffer();
        framesize = m_v4l2Capture.GetFrameSize();
        //printf("framesize=%d\n",framesize);
        if(framebuffer != NULL && framesize > 0)
            m_pServer->SendStreamToClient((char*)framebuffer,framesize);
       // mutex.Unlock();
    }
    //StreamServer server(1);
    //m_pServer = &server;
    //server.SetPort(40713);
    //server.StartRun();
    LOG_OUTPUT("MainThread End\n");

}
